package dao;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;

import sun.net.www.content.text.plain;
import connectUtil.ConnectionUtil;
import controller.EncyptPassword;
import entity.Visitor;

public class LoginDAO {
	private static Connection conn;
    private static CallableStatement callProc;
    private static ResultSet rs;
    
    public static Visitor checkVisistor(Visitor visitor) {
    	try {
    		conn = ConnectionUtil.getConnection();
    		callProc = conn.prepareCall("{call checkVisitor(?, ?)}");
    		callProc.setString(1, visitor.getUserName());
    		callProc.setString(2, visitor.getPassword());    		
    		rs = callProc.executeQuery();
    		
    		while (rs.next()) {
//    			System.out.println(" co du lieu roi !");
    			visitor.setId(rs.getInt("id"));
    			visitor.setUserName(rs.getString("userName"));
    			visitor.setPassword(rs.getString("password"));
    			visitor.setFirstName(rs.getString("firstName"));
    			visitor.setLastName(rs.getString("lastName"));
    			visitor.setGender(rs.getString("gender"));
    			visitor.setTelephone(rs.getString("telephone"));
    			visitor.setEmail(rs.getString("email"));
    			visitor.setArea(rs.getString("area"));
    			visitor.setHobby(rs.getString("hobby"));
    			visitor.setDescription(rs.getString("description"));
    			visitor.setRole(rs.getByte("role"));
    		}
    		System.out.println(" " + visitor.getUserName());
    	} catch (Exception ex) {
    		System.out.println(" CheckVisistor funtion says: " + ex.getMessage());
    	}
    	return visitor;
    }
    
    public static Visitor checkReVisistor(String visitorName) {
    	Visitor visitor = null;
    	try {
    		conn = ConnectionUtil.getConnection();
    		callProc = conn.prepareCall("{call checkReVisitor(?)}");
    		callProc.setString(1, visitorName);    		
    		rs = callProc.executeQuery();
    		
    		while (rs.next()) {
//    			System.out.println(" co du lieu roi !");
    			visitor = new Visitor();
    			visitor.setId(rs.getInt("id"));
    			visitor.setUserName(rs.getString("userName"));
    			visitor.setPassword(rs.getString("password"));    			
    			visitor.setFirstName(rs.getString("firstName"));
    			visitor.setLastName(rs.getString("lastName"));
    			visitor.setGender(rs.getString("gender"));
    			visitor.setTelephone(rs.getString("telephone"));
    			visitor.setEmail(rs.getString("email"));
    			visitor.setArea(rs.getString("area"));
    			visitor.setHobby(rs.getString("hobby"));
    			visitor.setDescription(rs.getString("description"));
    			visitor.setRole(rs.getByte("role"));
    		}
//    		System.out.println(" " + visitor.getUserName());
    	} catch (Exception ex) {
    		System.out.println("CheckReVisistor funtion says: " + ex.getMessage());
    	}
    	return visitor;
    }
    
    public static void main(String[] args) {
		Visitor visitor = new Visitor();
		visitor.setUserName("thanhpv7");
		visitor.setPassword(EncyptPassword.encryptMD5("RLJLCAMD"));
	    checkVisistor(visitor);
	    System.out.println(visitor.getId());
	}
}
